package com.frank.loadinglibrary.common.element;

import com.frank.loadinglibrary.base.baseanimations.BaseAnimationController;
import com.frank.loadinglibrary.base.baseelement.BaseElement;
import com.frank.loadinglibrary.base.baseelement.IComponentCallback;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;

import java.util.ArrayList;

public class LineScaleElement extends BaseElement implements BaseAnimationController.IAnimationController<AnimatorValue> {

    private Paint mPaint;

    private LineScaleParams parmas;
    private final int valueFive = 5;

    private LineScaleElement(LineScaleParams params, IComponentCallback call) {
        super();
        this.callback = call;
        this.parmas = params;
    }

    @Override
    public void initPaint() {
        super.initPaint();
        if (mPaint == null) {
            mPaint = new Paint();
        }
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setAntiAlias(true);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        float translateX = getWidth() / 11;
        float translateY = getHeight() / 2;
        for (int i = 0; i < valueFive; i++) {
            canvas.save();
            canvas.translate((2 + i * 2) * translateX - translateX / 2, translateY);
            canvas.scale(LineScaleParams.SCALE, parmas.scaleYFloats[i]);
            RectFloat rectF = new RectFloat(-translateX / 2, -getHeight() / 2.5f, translateX / 2, getHeight() / 2.5f);
            canvas.drawRoundRect(rectF, 5, 5, mPaint);
            canvas.restore();
        }
    }

    @Override
    public ArrayList<AnimatorValue> onCreateAnimators() {
        ArrayList<AnimatorValue> animators = new ArrayList<>();
        long[] delays = new long[]{500, 250, 0, 250, 500};
        for (int i = 0; i < valueFive; i++) {
            final int index = i;
            AnimatorValue scaleAnim = new AnimatorValue();
            scaleAnim.setDuration(parmas.durtion);
            scaleAnim.setLoopedCount(-1);
            scaleAnim.setDelay(delays[i]);
            scaleAnim.setValueUpdateListener((animatorValue, v) -> {
                if (v < 0.5f) {
                    parmas.scaleYFloats[index] = (float) (-1.4 * v + 1);
                } else {
                    parmas.scaleYFloats[index] = (float) (1.4 * v - 0.4);
                }
                if (callback != null) {
                    callback.reflushComponent();
                }
            });
            animators.add(scaleAnim);
        }
        return animators;
    }

    @Override
    public void onAnimationReset() {
        //do nothing because donot need to reset animation params
    }

    @Override
    public BaseAnimationController.IAnimationController<AnimatorValue> initAnimationController() {
        return this;
    }

    /**
     * 构建者
     */
    public static class LineScaleBuilder {
        LineScaleParams params;
        IComponentCallback call;

        public LineScaleBuilder(IComponentCallback c) {
            call = c;
            params = new LineScaleParams();
            setDurtion(LineScaleParams.BASE_DURTION_TIME);
        }

        public LineScaleBuilder setDurtion(int dur) {
            params.durtion = dur;
            return this;
        }

        public LineScaleElement build() {
            return new LineScaleElement(params, call);
        }
    }

    /***
     * 可配置参数类
     */
    public static class LineScaleParams {
        public static final float SCALE = 1.0f;
        //默认时长基数
        private static final int BASE_DURTION_TIME = 900;
        float[] scaleYFloats = new float[]{SCALE,
                SCALE,
                SCALE,
                SCALE,
                SCALE,};
        private int durtion = BASE_DURTION_TIME;

    }
}
